home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Char.sml.bak < prev    next >
Encoding:
Text File  |  1996-07-03  |  1.8 KB  |  58 lines  |  [TEXT/R*ch]

  1. (* Char -- new basis 1995-05-01 *)
  2.  
  3. type char = char
  4. (* Invariant: for c: char it holds that 0 <= ord c <= maxOrd *)
  5.  
  6. exception Chr = Chr
  7.  
  8. local 
  9.     prim_val sub_ : string -> int -> char = 2 "get_nth_char"
  10.     prim_val chr_ : int -> char          = 1 "identity";
  11. in
  12.     prim_val ord : char -> int           = 1 "identity";
  13.     val minChar = #"\000"
  14.     val maxChar = #"\255"
  15.     val maxOrd = 255;
  16.  
  17.     fun chr i = if i<0 orelse i>maxOrd then raise Chr else chr_ i;
  18.  
  19.     fun succ c = 
  20.     if c < maxChar then chr_(ord c + 1) else raise Chr;
  21.     fun pred c = 
  22.     if c > minChar then chr_(ord c - 1) else raise Chr;
  23.  
  24.     fun contains s c = 
  25.     let val stop = size s
  26.         fun h i = i < stop andalso (c = sub_ s i orelse h(i+1))
  27.     in h 0 end;
  28.  
  29.     fun notContains s c = not (contains s c);
  30.  
  31.     fun isLower c  = (#"a" <= c andalso c <= #"z")
  32.     fun isUpper c  = (#"A" <= c andalso c <= #"Z")
  33.     fun isDigit c  = (#"0" <= c andalso c <= #"9")
  34.     fun isAlpha c  = (isLower c orelse isUpper c)
  35.     fun isHexDigit c = (isDigit c orelse #"a" <= c andalso c <= #"f"
  36.             orelse #"A" <= c andalso c <= #"F")
  37.     fun isAlphaNum c  = (isAlpha c orelse isDigit c)
  38.     fun isPrint c  = c >= #" " andalso c <> #"\127" andalso c <> #"\255" 
  39.     fun isSpace c  = contains " \009\010\011\012\013" c;
  40.     fun isGraph c  = isPrint c andalso not (isSpace c)
  41.     fun isAscii c  = c <= #"\127"
  42.  
  43.     fun toLower c = 
  44.     if #"A" <= c andalso c <= #"Z" then chr_(ord c + 32)
  45.     else c;
  46.     fun toUpper c = 
  47.     if #"a" <= c andalso c <= #"z" then chr_(ord c - 32)
  48.     else c;
  49.  
  50.     fun cmp (x, y: char) = 
  51.     if x<y then LESS else if x>y then GREATER else EQUAL;
  52.  
  53.     val op <  = op < : char * char -> bool;
  54.     val op <= = op <= : char * char -> bool;
  55.     val op >  = op >  : char * char -> bool;
  56.     val op >= = op >= : char * char -> bool;
  57. end
  58.